from django.db import models
from django.contrib.auth.models import Group
from django.contrib.auth.models import AbstractUser



# from .models import User
class User(AbstractUser):
    user_id = models.CharField(max_length=60)
    mobile = models.CharField(max_length=60)
    remark = models.CharField(max_length=60)


class Permissions(models.Model):
    class Meta:
        verbose_name_plural = '权限管理'
        permissions = [
            ('host_view', '资源信息-查看权限'),
            ('host_manage', '资源信息-服务器管理权限'),
            ('asset_manage', '资源信息-资产管理权限'),
            ('system_manage', '资源信息-系统管理权限'),
            ('playbook_manage', '资源信息-playbook管理权限'),
            ('ansiblerole_manage', '资源信息-ansiblerole管理权限'),
            ('application_manage', '资源信息-application管理权限'),
            ('software_manage', '资源信息-软件包管理权限'),
            ('ansible_view', 'Ansible任务-查看ansible任务权限'),
            ('ansible_execute_playbook', 'Ansible任务-执行ansible权限'),
            ('ansible_trans', 'Ansible任务-分发数据包'),
            ('ansible_trans_views', 'Ansible任务-分发数据包任务查看权限'),
            ('auth_view', '权限管理-查看权限'),
            ('role_manage', '权限管理-角色管理权限'),
            ('user_manage', '权限管理-用户管理权限'),
            ('middle_monitor_view', '中间件监控-查看权限'),
            ('question_view', '问答-查看权限'),
        ]


class Playbooks(models.Model):
    remark = models.CharField(max_length=80, null=True, blank=True)
    nickName = models.CharField(max_length=80, null=True, blank=True)
    playbook = models.CharField(max_length=80, unique=True, null=True, blank=False)
    author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    status = models.CharField(max_length=20, null=True, blank=True)
    createTime = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = '可执行任务（playbook）'
        constraints = [models.UniqueConstraint(fields=['playbook'], name='unique_playbook'), ]

    def __str__(self):
        return self.playbook


class Applications(models.Model):
    module = models.CharField(max_length=200, null=True, blank=False)
    remark = models.CharField(max_length=200, null=True, blank=False)
    install_path = models.CharField(max_length=1000, null=True, blank=True)
    log_path = models.CharField(max_length=1000, null=True, blank=True)
    config_path = models.CharField(max_length=1000, null=True, blank=True)
    modules_port = models.CharField(max_length=200, null=True, blank=True)
    user_name = models.CharField(max_length=1000, null=True, blank=False)
    redundancy_type = models.CharField(max_length=200, null=True, blank=False)
    cluster_software = models.CharField(max_length=200, null=True, blank=False)
    storage = models.CharField(max_length=200, null=True, blank=False)

    # ip = models.ManyToManyField(HostsLists)

    class Meta:
        verbose_name_plural = 'applications'

    def __str__(self):
        return self.module


class HostsLists(models.Model):
    systemname = models.CharField(max_length=80, default='test')
    servicename = models.CharField(max_length=80, null=True, blank=True)
    clustername = models.CharField(max_length=80, null=True, blank=True)
    hostname = models.CharField(max_length=80, null=False, blank=True)
    location = models.CharField(max_length=40, null=True, blank=True)
    ip = models.CharField(max_length=80, null=False, blank=False)
    type = models.CharField(max_length=80, null=True, blank=True)
    groups = models.CharField(max_length=80, null=True, blank=True)
    in4A = models.BooleanField(default=False)
    manage = models.BooleanField(default=True)
    product = models.CharField(max_length=80, null=True, blank=True)
    application = models.ManyToManyField(Applications)

    class Meta:
        constraints = [models.UniqueConstraint(fields=['systemname', 'ip'], name='unique_host'), ]
        verbose_name_plural = '主机详情'

    def __str__(self):
        return self.hostname


class Softwares(models.Model):
    software = models.CharField(max_length=80, unique=True, null=True, blank=False)
    remark = models.CharField(max_length=80, null=True, blank=True)
    author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    status = models.CharField(max_length=20, null=True, blank=True)
    createTime = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = '软件包'

    def __str__(self):
        return self.software


class AnsibleRole(models.Model):
    ansiblerole = models.CharField(max_length=80, unique=True, null=True, blank=False)
    remark = models.CharField(max_length=80, null=True, blank=True)
    status = models.CharField(max_length=80, null=True, blank=False)
    createTime = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = 'ansiblerole'

    def __str__(self):
        return self.ansiblerole


class AnsibleRoleTasks(models.Model):
    ansiblerole = models.TextField(max_length=5000, null=True, blank=False)
    msg = models.TextField(max_length=5000, null=True, blank=True)
    system = models.TextField(max_length=5000, null=True, blank=True)
    createtime = models.DateTimeField(auto_now_add=True, null=True, blank=True)

    class Meta:
        ordering = ['-createtime']
        verbose_name_plural = 'ansibleroletask'

    def __str__(self):
        return self.ansiblerole


class Assets(models.Model):
    number = models.CharField(primary_key=True, max_length=80, null=False)
    label = models.CharField(max_length=80, null=True)
    name = models.CharField(max_length=80, null=True)
    category = models.CharField(max_length=80, null=True)
    description = models.CharField(max_length=200, null=True)
    serial = models.CharField(max_length=80, null=True)
    keys = models.CharField(max_length=80, null=True)
    factory = models.CharField(max_length=80, null=True)
    specification = models.CharField(max_length=200, null=True)
    unit = models.CharField(max_length=80, null=True)
    createdate = models.CharField(max_length=80, null=True)
    startdate = models.CharField(max_length=80, null=True)
    Apportiondate_rate = models.CharField(max_length=80, null=True)
    depmethod = models.CharField(max_length=20, null=True)
    useyear = models.IntegerField(default=0)
    remainmonth = models.IntegerField(default=0)
    isdep = models.CharField(max_length=8, null=True)
    isimort = models.CharField(max_length=8,null=True)
    cost = models.FloatField(max_length=40, default=0)
    netvalue = models.FloatField(max_length=40, default=0)
    netsales = models.FloatField(max_length=40, default=0)
    salvage = models.FloatField(max_length=40, default=0)
    depallowance_term = models.FloatField(max_length=40, default=0)
    depallowance_year = models.FloatField(max_length=40, default=0)
    depaccumulated = models.FloatField(max_length=40, default=0)
    predepallowance_term = models.FloatField(max_length=40, default=0)
    predepallowance_year = models.FloatField(max_length=40, default=0)
    predepaccumulated = models.FloatField(max_length=40, default=0)
    employeeNo = models.CharField(max_length=80, null=True)
    employeename = models.CharField(max_length=80, null=True)
    place = models.CharField(max_length=80, null=True)
    plcspec = models.CharField(max_length=80, null=True)
    oldno = models.CharField(max_length=80, null=True)
    oldlabel = models.CharField(max_length=80, null=True)
    buildstate = models.CharField(max_length=80, null=True)
    original = models.CharField(max_length=80, null=True)
    ticketno = models.CharField(max_length=80, null=True)
    projectno = models.CharField(max_length=80, null=True)
    projectname = models.CharField(max_length=80, null=True)
    extrainfo = models.CharField(max_length=80, null=True)
    costaccount = models.CharField(max_length=80, null=True)
    ctaccdesc = models.CharField(max_length=200, null=True)
    depaccount = models.CharField(max_length=80, null=True)
    depaccdesc = models.CharField(max_length=400, null=True)
    depaccumacc = models.CharField(max_length=80, null=True)
    depaccumaccdesc = models.TextField(null=True)
    budgetcmpyseg = models.CharField(max_length=80, null=True)
    budgetdept = models.CharField(max_length=80, null=True)
    bugetproj = models.CharField(max_length=80, null=True)
    reimbursement = models.CharField(max_length=80, null=True)
    fundaccounts = models.CharField(max_length=80, null=True)
    serviceplt = models.CharField(max_length=80, null=True)
    networklayer = models.CharField(max_length=80, null=True)
    isunionbuilt = models.CharField(max_length=8,null=True)
    budgetagreement = models.CharField(max_length=80, null=True)
    actnumber = models.CharField(max_length=80, null=True)
    deductmark = models.CharField(max_length=80, null=True)
    pptytype = models.CharField(max_length=80, null=True)

    class Meta:
        ordering = ['number']
        verbose_name_plural = '设备资产'

    def __str__(self):
        return self.name


class HostRes(models.Model):
    systemname = models.CharField(max_length=80, null=False)
    sship = models.CharField(max_length=80, null=False)
    hostname = models.CharField(max_length=80, null=True)
    ipaddrs = models.TextField(null=True)
    kernel = models.CharField(max_length=80, null=True)
    osrelease = models.CharField(max_length=80, null=True)
    productname = models.CharField(max_length=80, null=True)
    processorname = models.CharField(max_length=80, null=True)
    cpunum = models.IntegerField(null=True)
    cpurate = models.FloatField(max_length=20, null=True)
    memtotal = models.CharField(max_length=80, null=True)
    memrate = models.FloatField(max_length=20, null=True)
    fileUsage = models.TextField(null=True)
    AnsibleID = models.CharField(max_length=80, null=True)

    class Meta:
        constraints = [models.UniqueConstraint(fields=['systemname', 'sship'], name='unique_hostres'), ]
        verbose_name_plural = '主机资源'

    def __str__(self):
        return self.hostname


class SystemsLists(models.Model):
    systemname = models.CharField(max_length=80, default='test')
    remark = models.CharField(max_length=80, null=True, blank=True)
    department = models.CharField(max_length=80, null=True, blank=True)
    supersystem = models.CharField(max_length=80, null=True, blank=True)
    network_system_type = models.CharField(max_length=80, null=True, blank=True)
    social_influence_value = models.CharField(max_length=80, null=True, blank=True)
    social_influence_basis = models.CharField(max_length=80, null=True, blank=True)
    scale_service_scope_value = models.CharField(max_length=80, null=True, blank=True)
    scale_service_scope_basis = models.CharField(max_length=80, null=True, blank=True)
    service_important_value = models.CharField(max_length=80, null=True, blank=True)
    service_important_basis = models.CharField(max_length=80, null=True, blank=True)
    security_level_value = models.CharField(max_length=80, null=True, blank=True)
    security_level = models.CharField(max_length=80, null=True, blank=True)
    group = models.ManyToManyField(Group)
    control_host = models.CharField(max_length=80, null=True, blank=True)
    task = models.CharField(max_length=80, null=True, blank=True)
    agent_user = models.CharField(max_length=80, default='iotapp')
    agent_pass = models.CharField(max_length=80, default='test123')
    ssh_user = models.CharField(max_length=80, default='iotapp')
    ssh_pass = models.CharField(max_length=80, default='test123')
    application = models.ManyToManyField(Applications)

    class Meta:
        verbose_name_plural = '系统详情'

    def __str__(self):
        return self.systemname


class AnsibleTasks(models.Model):
    AnsibleID = models.CharField(max_length=80, unique=True, null=True, blank=True)
    CeleryID = models.CharField(max_length=80, unique=True, null=True, blank=True)
    SystemName = models.CharField(max_length=80, null=True, blank=True)
    TaskUser = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    TaskName = models.CharField(max_length=80, null=True, blank=True)
    playbook = models.CharField(max_length=80, null=True, blank=True)
    ExtraVars = models.TextField(blank=True, null=True)
    AnsibleResult = models.TextField(blank=True)
    CeleryResult = models.TextField(blank=True)
    Label = models.CharField(max_length=80, null=True, blank=True)
    CreateTime = models.DateTimeField(auto_now_add=True, null=True, blank=True)

    class Meta:
        ordering = ['-CreateTime']
        verbose_name_plural = 'Ansible 任务'

    def __str__(self):
        return self.AnsibleID


class ExtraVars(models.Model):
    Name = models.CharField(max_length=80, unique=True, null=True, blank=True)
    Content = models.TextField(blank=True)
    Remark = models.TextField(blank=True)

    class Meta:
        verbose_name_plural = '额外参数'

    def __str__(self):
        return self.Name
